package com.example.webcrawlercoursedesign;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 缓存管理
 */

public class NewsCache {
	private MySQLite mySQLite;

	//构造方法创建MySQLite对象对数据库进行操作
	NewsCache(Context context){
		this.mySQLite = new MySQLite(context);
	}

	//更新表数据
	public void updateCache(List<News> newsList, String table){
		SQLiteDatabase db = mySQLite.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + table, null);
		if (cursor.moveToNext()){
			db.execSQL("delete from " + table);
			db.execSQL("update sqlite_sequence SET seq=0 where name=?", new Object[]{table});
		}
		ContentValues values = new ContentValues();
		for (int i = 0; i < newsList.size(); i++){
			values.put("url", newsList.get(i).getNewsUrl());
			values.put("title", newsList.get(i).getNewsTitle());
			values.put("digest", newsList.get(i).getDesc());
			values.put("time", newsList.get(i).getNewsTime());
			values.put("imageUrl", newsList.get(i).getNewsImage());
			db.insert(table, null, values);
			Log.d("update", "updateCache");
		}
		cursor.close();
		db.close();
	}

	//读取表数据
	public List<News> loadCache(String table){
		List<News> newsList = new ArrayList<>();
		SQLiteDatabase db = mySQLite.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + table, null);
		if (!cursor.moveToFirst()){
			newsList = null;
		}
		else {
			do {
				String url = cursor.getString(1);
				String title = cursor.getString(2);
				String desc = cursor.getString(3);
				String time = cursor.getString(4);
				String imageUrl = cursor.getString(5);
				Log.d("load",title);
				News news = new News(title, url, desc, time, imageUrl);
				newsList.add(news);
			} while  (cursor.moveToNext());
		}
		cursor.close();
		db.close();
		return newsList;
	}

	public void saveInCache(List<News> newsList, String table){
		SQLiteDatabase db = mySQLite.getReadableDatabase();
		ContentValues values = new ContentValues();
		for (int i = 0; i < newsList.size(); i++){
			values.put("url", newsList.get(i).getNewsUrl());
			values.put("title", newsList.get(i).getNewsTitle());
			values.put("digest", newsList.get(i).getDesc());
			values.put("time", newsList.get(i).getNewsTime());
			values.put("imageUrl", newsList.get(i).getNewsImage());
			db.insert(table, null, values);
			Cursor cursor = db.rawQuery("select title from " + table,null);
			while (cursor.moveToNext()){
				Log.d("title", cursor.getString(0));
			}
			cursor.close();
		}
		db.close();
	}

	public void add2Cache(News news, String table){
		SQLiteDatabase db = mySQLite.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("url", news.getNewsUrl());
		values.put("title", news.getNewsTitle());
		values.put("digest", news.getDesc());
		values.put("time", news.getNewsTime());
		values.put("imageUrl", news.getNewsImage());
		db.insert(table, null, values);
		db.close();
	}

	//搜索表数据返回
	public List<News> searchInCache(String table, String key){
		List<News> result = new ArrayList<>();
		SQLiteDatabase db = mySQLite.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + table, null);
		if (!cursor.moveToFirst()){
			return null;
		}
		else {
			do {
				String url = cursor.getString(1);
				String title = cursor.getString(2);
				String desc = cursor.getString(3);
				String time = cursor.getString(4);
				String imageUrl = cursor.getString(5);
				if (title.contains(key)){
					News temp = new News(title, url, desc, time, imageUrl);
					result.add(temp);
				}
			}while (cursor.moveToNext());
		}
		cursor.close();
		db.close();
		return result;
	}

	public boolean isNetworkAvailable(Context context) {
		if (context != null) {
			// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			// 获取NetworkInfo对象
			NetworkInfo networkInfo = manager.getActiveNetworkInfo();
			//判断NetworkInfo对象是否为空
			if (networkInfo != null)
				return networkInfo.isAvailable();
		}
		return false;
	}
}